package com.hitemole.manager;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.hitemole.game.GameCore;
import com.hitemole.levelObject.Scores;

public class ScoreManager {
	private Stage stage;
	private Table table, table2;
	private GameCore core;
	private ImageButton restartButton;
	private ImageButton mainMenuButton;
	private TextureRegion scoreBoard;
	private Label label;
	private Scores score;
	private int scoreValue;

	public ScoreManager(GameCore core,Scores score2) {
		// TODO Auto-generated constructor stub
		this.core = core;
		stage = new Stage(GameManager.DEFAULT_SCREEN_WIDTH, GameManager.DEFAULT_SCREEN_WIDTH,false);
		table = new Table(core.getResourceManager().skinAsset.skin);
		table2 = new Table(core.getResourceManager().skinAsset.skin);
		table2.setPosition(table2.getX(), getStage().getHeight() + 50);
		//table2.addAction(Actions.moveTo(table2.getX(),  0,0.5f));
		table.setColor(table2.getColor().r, table2.getColor().g, table2.getColor().b, 0);
		table.addAction(Actions.alpha(1, 0.5f));
		score = new Scores(score2.getPlayerName(), 0);
		this.scoreValue=score2.getScore();
		restartButton = new ImageButton(
				this.core.getResourceManager().skinAsset.skin, "restart");
		restartButton.addListener(new InputListener(){
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				ScoreManager.this.core.restartGame();
				ScoreManager.this.core.setScreen(ScoreManager.this.core.getGameScreen());
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		mainMenuButton = new ImageButton(
				this.core.getResourceManager().skinAsset.skin, "menu");
		mainMenuButton.addListener(new InputListener(){
			@Override
			public boolean touchDown(InputEvent event, float x, float y,
					int pointer, int button) {
				// TODO Auto-generated method stub
				table.addAction(Actions.sequence(Actions.fadeOut(0.5f),new Action(){

					@Override
					public boolean act(float delta) {
						// TODO Auto-generated method stub
						ScoreManager.this.core.setScreen(ScoreManager.this.core.getMenuScreen());
						return true;
					}}));
				return super.touchDown(event, x, y, pointer, button);
			}
		});
		scoreBoard = new TextureRegion(
				core.getResourceManager().uiAsset.scoreBoardTexture);
		label = new Label(score.getStringScore(), core.getResourceManager().skinAsset.skin);
		label.setFontScale(0.8f);
		Image scoreImage = new Image(scoreBoard);
		table.setFillParent(true);
		table2.setFillParent(true);
		// table.add(scoreImage).uniform().fill().colspan(2).height(240);
		table.row();
		table2.add(label).colspan(2).size(60,75);
		table2.row().expand();
		table2.add(restartButton).uniform().space(100).expand().bottom().right();
		table2.add(mainMenuButton).uniform().expand().bottom().left();
		table.stack(scoreImage, table2).height(220);
		// table.row();
		// table.row();
//		table.debug(); // turn on all debug lines (table, cell, and widget)
//		table.debugTable(); // turn on only table lines
//		table2.debug(); // turn on all debug lines (table, cell, and widget)
//		table2.debugTable(); // turn on only table lines
//		stage.addActor(table);
		stage.addActor(table);
	}

	public Stage getStage() {
		return stage;
	}
	
	public void countScore(){
		score.setTargetScore(scoreValue);
	}

	public Scores getScore() {
		// TODO Auto-generated method stub
		return this.score;
	}
	
	public Label getScoreLabel(){
		return label;
	}

	public void setStage(Stage stage2) {
		// TODO Auto-generated method stub
		this.stage=stage2;
	}

	public Table getTable() {
		// TODO Auto-generated method stub
		return table;
	}
}
